const polyfill = require('./polyfill')
const parseConfig = require('./parseConfig')
const exportExcel = require('./excelExport')
const globalConfig = require('./config')
const startSpider = require('./startSpider')
const fs = require('fs')

polyfill()

module.exports = parseConfig('../config.txt')
.then((config) => {
  globalConfig.config = config
  console.info('开始抓取IOS信息')
  return startSpider('ios')
})
.then(list => {
  fs.writeFileSync('ios.json', JSON.stringify(list, null, 2))
  return exportExcel({
    title: globalConfig.config.iosExcelName,
    index: ['appname', 'count', 'currentScore', 'allScore', 'iphoneRank', 'ipadRank'],
    headers: {
      appname: '产品名称',
      count: '近一年下载量',
      currentScore: '当前评分',
      allScore: '所有版本评分',
      iphoneRank: 'iphone最新排名（教育榜）',
      ipadRank: 'ipad最新排名（教育榜）'
    },
    list
  })
})
.then(() => {
  console.info('IOS抓取完成')
  console.info('所有数据抓取完成')
})
.catch((err) => {
  console.error(err)
})
